home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / misc / gs261src.zip / gxpath2.c < prev    next >
C/C++ Source or Header  |  1993-05-13  |  7KB  |  258 lines

  1. /* Copyright (C) 1989, 1992 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* gxpath2.c */
  20. /* Path tracing procedures for Ghostscript library */
  21. #include "math_.h"
  22. #include "gx.h"
  23. #include "gserrors.h"
  24. #include "gxfixed.h"
  25. #include "gxarith.h"
  26. #include "gzpath.h"
  27.  
  28. /* Read the current point of a path. */
  29. int
  30. gx_path_current_point(const gx_path *ppath, gs_fixed_point *ppt)
  31. {    if ( !ppath->position_valid )
  32.       return_error(gs_error_nocurrentpoint);
  33.     /* Copying the coordinates individually */
  34.     /* is much faster on a PC, and almost as fast on other machines.... */
  35.     ppt->x = ppath->position.x, ppt->y = ppath->position.y;
  36.     return 0;
  37. }
  38.  
  39. /* Read the bounding box of a path. */
  40. int
  41. gx_path_bbox(gx_path *ppath, gs_fixed_rect *pbox)
  42. {    if ( ppath->bbox_set )
  43.     {    /* The bounding box was set by setbbox. */
  44.         *pbox = ppath->bbox;
  45.         return 0;
  46.     }
  47.     if ( ppath->first_subpath == 0 )
  48.        {    /* The path is empty, use the current point if any. */
  49.         gx_path_current_point(ppath, &pbox->p);
  50.         return gx_path_current_point(ppath, &pbox->q);
  51.        }
  52.     /* The stored bounding box may not be up to date. */
  53.     /* Correct it now if necessary. */
  54.     if ( ppath->box_last == ppath->current_subpath->last )
  55.        {    /* Box is up to date */
  56.         *pbox = ppath->bbox;
  57.        }
  58.     else
  59.        {    gs_fixed_rect box;
  60.         register segment *pseg = ppath->box_last;
  61.         if ( pseg == 0 )    /* box is uninitialized */
  62.            {    pseg = (segment *)ppath->first_subpath;
  63.             box.p.x = box.q.x = pseg->pt.x;
  64.             box.p.y = box.q.y = pseg->pt.y;
  65.            }
  66.         else
  67.            {    box = ppath->bbox;
  68.             pseg = pseg->next;
  69.            }
  70. /* Macro for adjusting the bounding box when adding a point */
  71. #define adjust_bbox(pt)\
  72.   if ( (pt).x < box.p.x ) box.p.x = (pt).x;\
  73.   else if ( (pt).x > box.q.x ) box.q.x = (pt).x;\
  74.   if ( (pt).y < box.p.y ) box.p.y = (pt).y;\
  75.   else if ( (pt).y > box.q.y ) box.q.y = (pt).y
  76.         while ( pseg )
  77.            {    switch ( pseg->type )
  78.                {
  79.             case s_curve:
  80. #define pcurve ((curve_segment *)pseg)
  81.                 adjust_bbox(pcurve->p1);
  82.                 adjust_bbox(pcurve->p2);
  83. #undef pcurve
  84.                 /* falls through */
  85.             default:
  86.                 adjust_bbox(pseg->pt);
  87.                }
  88.             pseg = pseg->next;
  89.            }
  90. #undef adjust_bbox
  91.         ppath->bbox = box;
  92.         ppath->box_last = ppath->current_subpath->last;
  93.         *pbox = box;
  94.        }
  95.     return 0;
  96. }
  97.  
  98. /* Test if a path has any curves. */
  99. int
  100. gx_path_has_curves(const gx_path *ppath)
  101. {    return ppath->curve_count != 0;
  102. }
  103.  
  104. /* Test if a path has any segments. */
  105. int
  106. gx_path_is_void(const gx_path *ppath)
  107. {    return ppath->first_subpath == 0;
  108. }
  109.  
  110. /* Test if a path is a rectangle. */
  111. /* If so, return its bounding box. */
  112. /* Note that this must recognize open as well as closed rectangles. */
  113. int
  114. gx_path_is_rectangle(const gx_path *ppath, gs_fixed_rect *pbox)
  115. {    const subpath *pseg0;
  116.     const segment *pseg1, *pseg2, *pseg3, *pseg4;
  117.     if (    ppath->subpath_count == 1 &&
  118.         (pseg1 = (pseg0 = ppath->first_subpath)->next) != 0 &&
  119.         (pseg2 = pseg1->next) != 0 &&
  120.         (pseg3 = pseg2->next) != 0 &&
  121.         ((pseg4 = pseg3->next) == 0 || pseg4->type == s_line_close) &&
  122.         ppath->curve_count == 0
  123.        )
  124.        {    fixed x0 = pseg0->pt.x, y0 = pseg0->pt.y;
  125.         fixed x2 = pseg2->pt.x, y2 = pseg2->pt.y;
  126.         if (    (x0 == pseg1->pt.x && pseg1->pt.y == y2 &&
  127.              x2 == pseg3->pt.x && pseg3->pt.y == y0) ||
  128.             (x0 == pseg3->pt.x && pseg3->pt.y == y2 &&
  129.              x2 == pseg1->pt.x && pseg1->pt.y == y0)
  130.            )
  131.            {    /* Path is a rectangle.  Return bounding box. */
  132.             if ( x0 < x2 )
  133.                 pbox->p.x = x0, pbox->q.x = x2;
  134.             else
  135.                 pbox->p.x = x2, pbox->q.x = x0;
  136.             if ( y0 < y2 )
  137.                 pbox->p.y = y0, pbox->q.y = y2;
  138.             else
  139.                 pbox->p.y = y2, pbox->q.y = y0;
  140.             return 1;
  141.            }
  142.        }
  143.     return 0;
  144. }
  145.  
  146. /* Translate an already-constructed path (in device space). */
  147. /* Don't bother to update the cbox. */
  148. int
  149. gx_path_translate(gx_path *ppath, fixed dx, fixed dy)
  150. {    segment *pseg;
  151. #define update_xy(pt)\
  152.   pt.x += dx, pt.y += dy
  153.     update_xy(ppath->bbox.p);
  154.     update_xy(ppath->bbox.q);
  155.     update_xy(ppath->position);
  156.     pseg = (segment *)(ppath->first_subpath);
  157.     while ( pseg )
  158.        {    switch ( pseg->type )
  159.            {
  160.         case s_curve:
  161.            {    curve_segment *pc = (curve_segment *)pseg;
  162.             update_xy(pc->p1);
  163.             update_xy(pc->p2);
  164.            }
  165.         default:
  166.             update_xy(pseg->pt);
  167.            }
  168.         pseg = pseg->next;
  169.        }
  170. #undef update_xy
  171.     return 0;
  172. }
  173.  
  174. /* Scale an already-constructed path (in device space). */
  175. /* Don't bother to update the cbox. */
  176. int
  177. gx_path_scale(gx_path *ppath, floatp sx, floatp sy)
  178. {    segment *pseg;
  179. #define update_xy(pt)\
  180.   pt.x = (fixed)(pt.x * sx), pt.y = (fixed)(pt.y * sy)
  181.     update_xy(ppath->bbox.p);
  182.     update_xy(ppath->bbox.q);
  183.     update_xy(ppath->position);
  184.     pseg = (segment *)(ppath->first_subpath);
  185.     while ( pseg )
  186.        {    switch ( pseg->type )
  187.            {
  188.         case s_curve:
  189.            {    curve_segment *pc = (curve_segment *)pseg;
  190.             update_xy(pc->p1);
  191.             update_xy(pc->p2);
  192.            }
  193.         default:
  194.             update_xy(pseg->pt);
  195.            }
  196.         pseg = pseg->next;
  197.        }
  198. #undef update_xy
  199.     return 0;
  200. }
  201.  
  202. /* Reverse a path. */
  203. /* We know ppath != ppath_old. */
  204. int
  205. gx_path_copy_reversed(const gx_path *ppath_old, gx_path *ppath, int init)
  206. {    const subpath *psub = ppath_old->first_subpath;
  207. #ifdef DEBUG
  208. if ( gs_debug['p'] )
  209.     gx_dump_path(ppath_old, "before reversepath");
  210. #endif
  211.     if ( init )
  212.         gx_path_init(ppath, ppath_old->memory_procs);
  213. nsp:    while ( psub )
  214.        {    const segment *pseg = psub->last;
  215.         const segment *prev;
  216.         int code = gx_path_add_point(ppath, pseg->pt.x, pseg->pt.y);
  217.         if ( code < 0 )
  218.            {    gx_path_release(ppath);
  219.             return code;
  220.            }
  221.         for ( ; ; pseg = prev )
  222.            {    prev = pseg->prev;
  223.             switch ( pseg->type )
  224.                {
  225.             case s_start:
  226.                 /* Finished subpath */
  227.                 if ( psub->closed )
  228.                     code = gx_path_close_subpath(ppath);
  229.                 psub = (const subpath *)psub->last->next;
  230.                 goto nsp;
  231.             case s_curve:
  232.                {    const curve_segment *pc = (const curve_segment *)pseg;
  233.                 code = gx_path_add_curve(ppath,
  234.                     pc->p2.x, pc->p2.y,
  235.                     pc->p1.x, pc->p1.y,
  236.                     prev->pt.x, prev->pt.y);
  237.                 break;
  238.                }
  239.             case s_line:
  240.             case s_line_close:
  241.                 code = gx_path_add_line(ppath, prev->pt.x, prev->pt.y);
  242.                 break;
  243.                }
  244.             if ( code )
  245.                {    gx_path_release(ppath);
  246.                 return code;
  247.                }
  248.            }
  249.         /* not reached */
  250.     }
  251.     ppath->position = ppath_old->position;        /* restore current point */
  252. #ifdef DEBUG
  253. if ( gs_debug['p'] )
  254.     gx_dump_path(ppath, "after reversepath");
  255. #endif
  256.     return 0;
  257. }
  258.